DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.
Проблема: дублирование валидации
// Пример плохого кода if (user.Age < 18) throw new Exception("User must be at least 18");
...
if (user.Age < 18) return BadRequest("User must be at least 18");
При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.
Решение: вынос логики в общее правило
public static class ValidationRules { public static bool IsAdult(User user) => user.Age >= 18; }
Теперь в коде
if (!ValidationRules.IsAdult(user)) throw new Exception("User must be at least 18");
// И в другом месте: if (!ValidationRules.IsAdult(user)) return BadRequest("User must be at least 18");
Альтернатива: использование FluentValidation или DataAnnotations
public class User { [Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")] public int Age { get; set; } }
Где чаще всего нарушают DRY в .NET:
• Повторяющиеся SQL-запросы и фильтры • Повторение одинаковых exception'ов, логов, сообщений • Дублирование конфигурации • UI-формы и компоненты
DRY (Don’t Repeat Yourself) — принцип, согласно которому каждый фрагмент знания должен существовать в системе только в одном месте. Никакого копипаста и дублирующей логики.
Проблема: дублирование валидации
// Пример плохого кода if (user.Age < 18) throw new Exception("User must be at least 18");
...
if (user.Age < 18) return BadRequest("User must be at least 18");
При изменении правила — нужно помнить обновить его везде. Ловушка копипаста.
Решение: вынос логики в общее правило
public static class ValidationRules { public static bool IsAdult(User user) => user.Age >= 18; }
Теперь в коде
if (!ValidationRules.IsAdult(user)) throw new Exception("User must be at least 18");
// И в другом месте: if (!ValidationRules.IsAdult(user)) return BadRequest("User must be at least 18");
Альтернатива: использование FluentValidation или DataAnnotations
public class User { [Range(18, int.MaxValue, ErrorMessage = "User must be at least 18")] public int Age { get; set; } }
Где чаще всего нарушают DRY в .NET:
• Повторяющиеся SQL-запросы и фильтры • Повторение одинаковых exception'ов, логов, сообщений • Дублирование конфигурации • UI-формы и компоненты
Durov said on his Telegram channel today that the two and a half year blockchain and crypto project has been put to sleep. Ironically, after leaving Russia because the government wanted his encryption keys to his social media firm, Durov’s cryptocurrency idea lost steam because of a U.S. court. “The technology we created allowed for an open, free, decentralized exchange of value and ideas. TON had the potential to revolutionize how people store and transfer funds and information,” he wrote on his channel. “Unfortunately, a U.S. court stopped TON from happening.”
The seemingly negative pandemic effects and resource/product shortages are encouraging and allowing organizations to innovate and change.The news of cash-rich organizations getting ready for the post-Covid growth economy is a sign of more than capital spending plans. Cash provides a cushion for risk-taking and a tool for growth.